package com.uuuo.retrofit2.Converter;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by JiaTao on 2018/5/16.
 * Email: 535318720@qq.com
 * Version: 1.0
 * Description:
 */
public class BaseSubscriber<T> implements Observer<T> {
    private static final String TAG = "BaseSubscriber";
    protected Context mContext;

    public BaseSubscriber(Context context) {
        this.mContext = context;
    }

    @Override
    public void onSubscribe(Disposable d) {
        Log.i(TAG, "onSubscribe--->" + Thread.currentThread().getName());
    }

    @Override
    public void onNext(T t) {
        Log.i(TAG, "onNext--->" + Thread.currentThread().getName());
    }

    @Override
    public void onComplete() {
        Log.i(TAG, "onComplete--->" + Thread.currentThread().getName());
    }

    @Override
    public void onError(final Throwable e) {
        Log.i(TAG, "onError--->" + Thread.currentThread().getName());

        if (e instanceof HttpException) {
            Toast.makeText(mContext, "服务器内部发生错误,请稍候重试", Toast.LENGTH_SHORT).show();
        } else if (e instanceof IOException) {
            // A network or conversion error happened
            Toast.makeText(mContext, "服务器连接失败,请稍候重试", Toast.LENGTH_SHORT).show();
        } else if (e instanceof JsonSyntaxException) {
            Toast.makeText(mContext, "数据解析错误", Toast.LENGTH_SHORT).show();
        } else if (e instanceof ApiException) {
            ApiException exception = (ApiException) e;
            if (exception.isTokenExpried()) {
                //处理token失效对应的逻辑
            } else {
                Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(mContext, "发生异常错误,异常信息:\n" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    public static <U> ObservableTransformer<HttpResult<U>, U> applySchedulers() {
        return new ObservableTransformer<HttpResult<U>, U>() {
            @Override
            public ObservableSource<U> apply(Observable<HttpResult<U>> upstream) {
                return upstream.flatMap(new Function<HttpResult<U>, ObservableSource<U>>() {
                    @Override
                    public ObservableSource<U> apply(HttpResult<U> uHttpResult) {
                        return Observable.just(uHttpResult.data);

                    }
                }).subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }


}